package com.hdaccp.xingmu.controller;

import com.hdaccp.xingmu.dao.OrderDao;
import com.hdaccp.xingmu.entity.House;
import com.hdaccp.xingmu.entity.Orderhao;
import com.hdaccp.xingmu.noentity.random;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.*;

@Controller
@RequestMapping("/order")
public class OrderCon {
    @Autowired
    private OrderDao orderDao;
    @PersistenceContext
    private EntityManager entityManager;

    @RequestMapping("/add")
    @ResponseBody
    public Orderhao oo(Orderhao orderhao){

        random random = new random();
        String ran = random.r();

        orderhao.setOrderid(ran);
        Orderhao o = orderDao.save(orderhao);
        return o;
    }

    @RequestMapping("/list")
    @ResponseBody
    public Map<String,Object> list(String date,String danhao,int pageNumber,int pageSize){
        System.out.println("danhao--------"+danhao);


        String sql = "from Orderhao where 1=1";

        if(date != null && !date.equals("")){
            String[] ss = date.split(" - ");
            sql+=" and date > '"+ss[0]+"' and date < '"+ss[1]+"'";
        }
        if(danhao!=null && !danhao.equals("")){
            sql+=" and orderid = "+danhao;
        }
        System.out.println(sql);


        Query query1 = entityManager.createQuery(sql);
        List<Orderhao> li = query1.getResultList();



        //sql+=" limit "+(pageNumber-1*pageSize)+","+pageSize;



        Query query = entityManager.createQuery(sql);

        query.setFirstResult((pageNumber-1)*pageSize);
        query.setMaxResults(pageSize);

        List<Orderhao> list = query.getResultList();

        Map<String,Object> map=new HashMap<String,Object>();
        map.put("total",li.size());
        map.put("rows",list);

        return map;


    }


}
